Gef.ns('Gef.jbs');

Gef.jbs.ExtEditor = Gef.extend(Object, {
			constructor : function() {
				// 创建模型工厂管理器
				this.modelFactory = new Gef.jbs.JBSModelFactory();
				// 创建编辑器工厂管理器
				this.editPartFactory = new Gef.jbs.JBSEditPartFactory();
				// 编辑数据缓存区
				this.editDomain = this.createEditDomain();

				this.graphicalViewer = this.createGraphicalViewer();
			},

			createGraphicalViewer : function() {
				return new Gef.jbs.ExtGraphicalViewer(this);
			},

			getPaletteHelper : function() {
				if (!this.paletteHelper) {
					this.paletteHelper = new Gef.jbs.ExtPaletteHelper(this);
				}
				return this.paletteHelper;
			},

			addSelectionListener : function(selectionListener) {
				this.getGraphicalViewer().getBrowserListener().selectionListenerTracker.addSelectionListener(selectionListener);
			},

			enable : function() {
				this.getGraphicalViewer().getBrowserListener().enable();
			},

			disable : function() {
				this.getGraphicalViewer().getBrowserListener().disable();
			},
			serial : function() {
				var $ = this.getGraphicalViewer().getContents().getModel(), _ = new Gef.jbs.xml.JBSSerializer($), A = _.serialize();
				return A
			},
			clear : function() {
				var D = this.getGraphicalViewer(), A = D.getContents(), C = D.getBrowserListener(), _ = D.getEditDomain().getCommandStack(), $ = C.getSelectionManager();
				$.selectAll();
				var B = A.getRemoveNodesCommand({
							role : {
								nodes : $.getSelectedNodes()
							}
						});
				_.execute(B);
				$.clearAll();
				this.editDomain.editPartRegistry = []
			},
			reset : function() {
				this.clear();
				var A = this.getGraphicalViewer(), $ = A.getEditDomain().getCommandStack();
				$.flush();
				this.getModelFactory().reset();
				var _ = A.getContents();
				_.text = "untitled";
				_.key = null;
				_.description = null
			},
			resetAndOpen : function($) {
				this.reset();
				var A = new Gef.jbs.xml.JBSDeserializer($), _ = A.decode();
				this.getGraphicalViewer().setContents(_);
				this.updateModelFactory();
				this.getGraphicalViewer().getContents().refresh()
			},
			init : function($) {
				var _ = $.getObject();
				this.getGraphicalViewer().setContents(_);
				this.editDomain = new Gef.gef.EditDomain();
				this.editDomain.setEditor(this);
				this.updateModelFactory();
			},
			updateModelFactory : function() {
				var A = this.getGraphicalViewer().getContents().getModel(), _ = this.getModelFactory(), $ = {};
				Gef.each(A.getChildren(), function(E) {
							var H = E.getType(), C = E.text;
							if (!C)
								return true;
							var A = _.getTypeName(H), D = A + " ";
							if (C.indexOf(D) != 0)
								return true;
							var G = C.substring(D.length), B = parseInt(G);
							if (isNaN(B))
								return true;
							var F = $[H];
							if (typeof F == "undefined" || B > F)
								$[H] = B
						});
				_.map = $
			},
			setWorkbenchPage : function($) {
				this.workbenchPage = $
			},

			createPaletteHelper : Gef.emptyFn,
			render : function() {
				this.getGraphicalViewer().render()
			},

			getGraphicalViewer : function() {
				return this.graphicalViewer
			},
			setGraphicalViewer : function($) {
				this.graphicalViewer = $
			},
			createEditDomain : function() {
				var $ = new Gef.gef.EditDomain();
				$.setEditor(this);
				return $
			},
			setEditDomain : function($) {
				this.editDomain = $
			},
			getEditDomain : function() {
				return this.editDomain
			},
			getModelFactory : function() {
				return this.modelFactory
			},
			setModelFactory : function($) {
				this.modelFactory = $
			},
			getEditPartFactory : function() {
				return this.editPartFactory
			},
			setEditPartFactory : function($) {
				this.editPartFactory = $
			},
			addWidth : function($) {
				if (Gef.isVml)
					;
				else {
					var _ = document.getElementById("_Gef_0"), A = parseInt(_.getAttribute("width"), 10);
					_.setAttribute("width", A + $)
				}
			},
			addHeight : function($) {
				if (Gef.isVml)
					;
				else {
					var A = document.getElementById("_Gef_0"), _ = parseInt(A.getAttribute("height"), 10);
					A.setAttribute("height", _ + $)
				}
			},
			getWorkbenchPage : Gef.emptyFn
		});


Gef.ns("Gef.jbs");

Gef.jbs.ExtGraphicalViewer = Gef.extend(Object, {
			constructor : function($) {
				this.editor = $;
				this.rootEditPart = this.createRootEditPart();
				// 为浏览器增加事件监听
				this.browserListener = new Gef.gef.tracker.BrowserListener(this)
			},
			render : function() {
				this.canvasEl = Ext.getDom('__gef_jbs_center__');
				this.rootEditPart.render();
				this.rendered = true;
			},

			getPaletteLocation : function() {
				if (!this.paletteLocation) {
					var paletteBox = Ext.get('__gef_jbs_palette__').getBox();
					this.paletteLocation = {
						x : paletteBox.x,
						y : paletteBox.y,
						w : paletteBox.width,
						h : paletteBox.height
					};
				}
				return this.paletteLocation;
			},

			getCanvasLocation : function() {
				// if (!this.canvasLocation) {
				var box = Ext.get('__gef_jbs_center__').getBox();
				var scroll = Ext.get('__gef_jbs_center__').getScroll();
				this.canvasLocation = {
					x : box.x,
					y : box.y,
					w : box.width,
					h : box.height
				};
				// }
				return this.canvasLocation;
			},
			getActivePalette : function() {
				return this.editor.getPaletteHelper().getActivePalette()
			},
			createRootEditPart : function() {
				return new Gef.gef.support.DefaultRootEditPart(this)
			},
			getEditDomain : function() {
				return this.editor.getEditDomain()
			},
			getEditPartFactory : function() {
				return this.editor.editPartFactory
			},
			setContents : function(_) {
				var $ = _, D = $.getType();
				var A = this.editor.getEditPartFactory().createEditPart(D);
				A.setModel($);
				this.rootEditPart.setContents(A);
			},
			getLayer : function($) {
				return this.rootEditPart.getFigure().getLayer($)
			},
			getPaletteConfig : function(_, $) {
				return this.editor.getPaletteHelper().getPaletteConfig(_, $)
			},
			getEditor : function() {
				return this.editor
			},
			getBrowserListener : function() {
				return this.browserListener
			},
			getContents : function() {
				return this.rootEditPart.getContents()
			},

			getRootEditPart : function() {
				return this.rootEditPart
			},
			setRootEditPart : function($) {
				this.rootEditPart = $
			},
			setEditDomain : Gef.emptyFn
		});

Gef.jbs.ExtPaletteHelper = Gef.extend(Object, {
			createSource : function() {
				return {
					select : {
						text : 'select',
						creatable : false
					},
					transition : {
						text : 'transition',
						creatable : false,
						isConnection : true
					},
					start : {
						text : 'start',
						w : 48,
						h : 48
					},
					end : {
						text : 'end',
						w : 48,
						h : 48
					},
					fork : {
						text : 'fork',
						w : 48,
						h : 48
					},
					join : {
						text : 'join',
						w : 48,
						h : 48
					},
					auto : {
						text : 'auto',
						w : 90,
						h : 50
					}
				};
			},

			getSource : function() {
				if (!this.source) {
					this.source = this.createSource();
				}
				return this.source;
			},

			render : Gef.emptyFn,

			changeActivePalette : function(paletteConfig) {
				var el = null;
				if (this.getActivePalette()) {
					var oldActivePaletteId = this.getActivePalette().text;
					el = document.getElementById(oldActivePaletteId + '-img');
					el.style.border = '';
				}
				this.setActivePalette(paletteConfig);

				el = document.getElementById(paletteConfig.text + '-img');
				el.style.border = '1px dotted black';
			},

			resetActivePalette : function() {
				this.changeActivePalette({
							text : 'select'
						});
			},

			getPaletteConfig : function(p, t) {
				var id = t.parentNode.id;

				if (!id) {
					return null;
				}

				var source = this.getSource();
				var paletteConfig = this.getSource()[id];

				if (!paletteConfig) {
					return null;
				}

				this.changeActivePalette(paletteConfig);

				if (paletteConfig.creatable === false) {
					return null;
				}

				return paletteConfig;
			},
			constructor : function($) {
				this.editor = $
			},
			getActivePalette : function() {
				return this.activePalette
			},
			setActivePalette : function($) {
				this.activePalette = $
			}
		});

Gef.ns('Gef.jbs');

Gef.jbs.ExtSelectionListener = Gef.extend(Object, {
			constructor : function(propertyGrid) {
				this.propertyGrid = propertyGrid;
			},

			selectNode : function(editPart) {
				var node = editPart.getModel();
				if (this.propertyGrid) {
					this.propertyGrid.updateForm(node);
				}

				this.model = node;
			},

			selectConnection : function(editPart) {
				var connection = editPart.getModel();
				if (this.propertyGrid) {
					this.propertyGrid.updateForm(connection);
				}

				this.model = connection;
			},

			selectDefault : function(editPart) {
				var process = editPart.getModel();
				if (this.propertyGrid) {
					this.propertyGrid.updateForm(process);
				}

				this.model = process;
			},

			setEditor : function(editor) {
				this.editor = editor;

				this.model = editor.getGraphicalViewer().getContents().getModel();
			},

			editText : function(model, text) {
				var command = new Gef.gef.command.EditTextCommand(model, text);
				this.editor.getEditDomain().getCommandStack().execute(command);
			},

			getModel : function() {
				return this.model;
			},
			selectionChanged : function(A, $, _) {
				if (A.length == 1) {
					var B = A[0];
					if (B == _)
						this.selectDefault(_);
					else if (B.getClass() == "node")
						this.selectNode(B);
					else
						this.selectConnection(B)
				} else
					this.selectDefault(_)
			}
		});